package cn.itcast.wanxinp2p.account.service.impl;

import cn.itcast.wanxinp2p.common.domain.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class SmsService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${sms.url}")
    private String smsUrl;

    /**
     * 校验短信验证码
     * @param key 校验标识
     * @param code 验证码
     * @return 校验结果
     */
    public boolean verifySmsCode(String key, String code) {
        String url = smsUrl + "/verify?name=sms&verificationKey=" + key + "&verificationCode=" + code;
        ResponseEntity<RestResponse<Boolean>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, null, new ParameterizedTypeReference<RestResponse<Boolean>>() {});
        RestResponse<Boolean> restResponse = responseEntity.getBody();
        if (restResponse.getCode() == 0 && restResponse.getResult()) {
            return true;
        }
        return false;
    }
}